#############################################################################
# Modeling Issue Competence Over Time
# Last Updated: 2025-12
# R version 4.5.1 (2025-06-13) -- "Great Square Root"
# Input:
#         1. issue_ownership_rev_v2.Rdata (Raw data)
# Output:
#         1. full_base_rev_v2.Rdata (Model 1 in Table 1)
#         2. full_base_pid_rev_v2.Rdata (Model 2 in Table 1)
#         3. full_topic_rev_v2.Rdata (Model 3 in Table 1)
#         4. full_topic_pid_rev_v2.Rdata (Model 4 in Table 1)
#         5. full_topic_biannual_interact_pid_rev_v2.Rdata (Dynamic Issue Competence model; Table A2)
# Note: Reviewers should modify the code by increasing the number of iterations
#       to the full value stated in the paper to obtain the correct results. 
#       In the replication code, we set the number of iterations to 500 
#       to avoid excessive computation time (But, it still takes about 6-8 hours of running).
############################################################################
library(brms)

load("issue_ownership_rev_v2.Rdata")

#Model 1
  full_base <- brm(
  	comp ~ 
  		1,
  	data = dat,
  	family = categorical(),
  	chains = 4,
  	cores = 4
  )
  
  save(
  	full_base, 
  	file = "full_base_rev_v2.Rdata"
  )

#Model 2
  full_base_pid <- brm(
    comp ~ 
      1 + 
      pid,
    data = dat,
    family = categorical(),
    chains = 4,
    cores = 4
  )
  
  save(
    full_base_pid, 
    file = "full_base_pid_rev_v2.Rdata"
  )

#Model 3
  full_topic <- brm(
    comp ~ 
      1 + 
      (1 | cap_full),
    data = dat,
    family = categorical(),
    chains = 4,
    cores = 4,
    iter = 500
  )
  
  save(
    full_topic,
    file = "full_topic_rev_v2.Rdata"
  )

#Model 4
  full_topic_pid <- brm(
    comp ~ 
      1 + 
      pid + 
      (1 | cap_full),
    data = dat,
    family = categorical(),
    chains = 4,
    cores = 4,
    iter = 500
  )
  
  save(
    full_topic_pid,
    file = "full_topic_pid_rev_v2.Rdata"
  )

# Dynamic Issue Competence model; Table A2
  full_topic_biannual_interact_pid <- brm(
    comp ~ 
      1 + 
      pid +
      (1 | halbjahr) +
      (1 | cap_full) +
      (1 | cap_full:halbjahr),
    data = dat,
    family = categorical(),
    chains = 4,
    cores = 4,
    iter = 500
  )
  
  save(
    full_topic_biannual_interact_pid,
    file = "full_topic_biannual_interact_pid_rev_v2.Rdata"
  )